package com.hkbigdata.sybx.mapper;

import com.hkbigdata.sybx.bean.EmergencyContact;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author yangjing
 * @creat 2025-06-03-16:50
 * @email 3010335359@qq.com
 */
@Mapper
public interface EmergencyContactMapper {

    /**
     * 添加紧急联系人
     */
    @Insert("INSERT INTO emergency_contact " +
            "(contact_name, contact_phone, user_id) " +
            "VALUES (#{contactName}, #{contactPhone}, #{userId})")
    @Options(useGeneratedKeys = true, keyProperty = "contactId", keyColumn = "contact_id")
    int insert(EmergencyContact contact);

    /**
     * 根据ID删除紧急联系人
     */
    @Delete("DELETE FROM emergency_contact WHERE contact_id = #{contactId}")
    int deleteById(Long contactId);

    /**
     * 根据用户ID删除所有紧急联系人
     */
    @Delete("DELETE FROM emergency_contact WHERE user_id = #{userId}")
    int deleteByUserId(String userId);

    /**
     * 更新紧急联系人信息
     */
    @Update("UPDATE emergency_contact " +
            "SET contact_name = #{contactName}, " +
            "contact_phone = #{contactPhone} " +
            "WHERE contact_id = #{contactId}")
    int update(EmergencyContact contact);

    /**
     * 根据ID查询紧急联系人
     */
    @Select("SELECT " +
            "contact_id AS contactId, " +
            "contact_name AS contactName, " +
            "contact_phone AS contactPhone, " +
            "user_id AS userId " +
            "FROM emergency_contact WHERE contact_id = #{contactId}")
//    @Select("SELECT * FROM emergency_contact WHERE contact_id = #{contactId}")
    EmergencyContact selectById(Long contactId);

    /**
     * 根据用户userID查询所有紧急联系人
     */
    @Select("SELECT " +
            "contact_id AS contactId, " +
            "contact_name AS contactName, " +
            "contact_phone AS contactPhone, " +
            "user_id AS userId " +
            "FROM emergency_contact WHERE user_id = #{userId}")
//    @Select("SELECT * FROM emergency_contact WHERE user_id = #{userId}")
    List<EmergencyContact> selectByUserId(String userId);

    /**
     * 查询所有紧急联系人
     */
    @Select("SELECT " +
            "contact_id AS contactId, " +
            "contact_name AS contactName, " +
            "contact_phone AS contactPhone, " +
            "user_id AS userId " +
            "FROM emergency_contact ORDER BY user_id, contact_id ASC")
//    @Select("SELECT * FROM emergency_contact ORDER BY user_id, contact_id ASC")
    List<EmergencyContact> selectAll();

}
